package nciae.db.conf;


import org.jodconverter.local.office.LocalOfficeManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;

@Configuration
public class OfficeManagerConfig {

    private LocalOfficeManager officeManager;

    @Bean
    public LocalOfficeManager officeManager() {
        if (officeManager == null) {
            officeManager = LocalOfficeManager.builder().install().build();
            try {
                officeManager.start();
                System.out.println("LibreOffice 已启动");
            } catch (Exception e) {
                System.err.println("启动 LibreOffice 失败：" + e.getMessage());
                throw new RuntimeException("启动 LibreOffice 失败", e);
            }
        }
        return officeManager;
    }

    @PreDestroy
    public void stopOfficeManager() {
        if (officeManager != null) {
            try {
                officeManager.stop();
                System.out.println("LibreOffice 已关闭");
            } catch (Exception e) {
                System.err.println("关闭 LibreOffice 失败：" + e.getMessage());
            }
        }
    }
}
